package com.example.sqlliteapp;
import android.content.Context;
import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class XunfeiApiClient {
    private static XunfeiApiClient instance;
    private final OkHttpClient client;
    private final Context context;
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static synchronized XunfeiApiClient getInstance(Context context) {
        if (instance == null) {
            instance = new XunfeiApiClient(context);
        }
        return instance;
    }
    private XunfeiApiClient(Context context) {
        this.context = context.getApplicationContext();
        this.client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS) // 大模型响应可能需要更长时间
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
    }
    public void sendMessage(String query, final ApiCallback callback) {
        try {
            String baseUrl = context.getString(R.string.xunfei_base_url);
            String apiPassword = context.getString(R.string.xunfei_api_key);
            JSONObject requestBody = new JSONObject();
            requestBody.put("model", "generalv3.5");
            JSONArray messages = new JSONArray();
            JSONObject message = new JSONObject();
            message.put("role", "user");
            message.put("content", query);
            messages.put(message);
            requestBody.put("messages", messages);
            requestBody.put("stream", false);
            Request request = new Request.Builder()
                    .url(baseUrl)
                    .post(RequestBody.create(requestBody.toString(), JSON))
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Authorization", "Bearer " + apiPassword)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    callback.onFailure("网络错误: " + e.getMessage());
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    try (ResponseBody body = response.body()) {
                        if (!response.isSuccessful()) {
                            callback.onFailure("API错误: " + response.code() + " - " + body.string());
                            return;
                        }
                        callback.onSuccess(body.string());
                    }
                }
            });
        } catch (JSONException e) {
            callback.onFailure("请求构造失败: " + e.getMessage());
        }
    }
    public interface ApiCallback {
        void onSuccess(String response);
        void onFailure(String error);
    }
}